console.log('新增 - 保存');
const form = this.getApiPage().retrievePlugin('base-forms');
const data = form.getFormData('form_19');
console.log('form.data', data);

const levels = this.getApiPage().getDataById('744574925553008640')?.data || [];
const types = this.getApiPage().getDataById('744575296061046784')?.data || [];
const eventNatures = this.getApiPage().getDataById('756194853578080256')?.data || [];
const projects = this.getApiPage().getDataById('848159656336424960')?.data || [];
const sourceSystem = this.getApiPage().getDataById('800764940934709248')?.data || [];
const monitorList = this.getApiPage().getApiWidgetByAliasName('edit-form').widgetObj;
const prefix = this.pageObj.getVariables().find(v => v.name === "environmentType")?.value;
const project = projects.find(v => v.value === obj.projectCode);
const obj = Object.keys(data).reduce((acc, key) => {
  if (data[key] !== '') acc[key] = data[key];
  return acc;
}, {});

obj.eventType = types.find(v => v.value === obj.eventTypeCode)?.name;
obj.eventGrade = levels.find(v => v.value === obj.eventGradeCode)?.name;
obj.eventQuality = eventNatures.find(v => v.value === obj.eventQualityCode)?.name;
obj.staffOnDuty = JSON.parse(sessionStorage.getItem('owner')).username;
obj.sourceDevice = Array.isArray(monitorList?.tempData) && monitorList?.tempData.find(v => v.value === obj.sourceDeviceId)?.name || '';
obj.source = sourceSystem.find(v => v.value === obj.sourceSystemCode)?.name;
const eventDetails = JSON.stringify({thumbnailPath: obj.thumbnailPath, jpbPath: obj.jpbPath});
delete obj.eventPoint;
delete obj.thumbnailPath;
delete obj.jpbPath;
const params = {
  ...obj,
  // 演练来源
  projectName: project?.name,
  projectCode: project?.value,
  projectId: project?.id,
  eventDetails
};
console.log('save.....', params);
const options = {
  method: 'POST',
  headers: {
    "Content-type": "application/json",
  },
  body: JSON.stringify(params)
};

fetch(`/${prefix}Event/event/insertEvent`, options)
  .then(res => res.json())
  .then(json => {
    console.log(json);
    if (json?.resultCode === '000000') {
      const gis = this.getApiPage().getApiWidgetByAliasName('三维组件');
      gis.widgetObj.setIconPosition({ right: 20, top: 100 });
      gis.widgetObj.setModalPosition({ right: 405, bottom: 20 });
      gis.widgetObj.gisMap.showOrHideAllArea(false);
      this.getApiPage().getApiWidgetsByClassName('edit')?.forEach(v => v.hideWidget());
      this.getApiPage().setVariables([{ name: 'update', value: new Date().toLocaleTimeString() }]);
      monitorList.resetForm('form_19');
      monitorList.render()
    }
  });
